package Tools.JUCTools.Completable_Future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static ProcessThread.util.Sleeper.sleep;

public class init {

    public static ExecutorService executor = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //fun1();
        //fun2();
        //fun3();
        //fun4();
        //fun5();
        fun6();
    }


    public static void fun1() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }, init.executor);

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, init.executor);
        System.out.println("main......end....." + future1.get());
    }

    public static void fun2() throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).whenComplete((res, exception) -> {
            //虽然能得到异常信息，但是没法修改返回数据
            System.out.println("异步任务成功完成了...结果是：" + res + "异常是：" + exception);
        }).exceptionally(throwable -> {
            System.out.println("异步任务成功完成了..." + "异常是：" + throwable);
            //可以感知异常，同时返回默认值
            return 2;
        });

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).exceptionally(throwable -> {
            System.out.println("异步任务成功完成了..." + "异常是：" + throwable);
            //可以感知异常，同时返回默认值
            return 2;
        });
        Integer integer = future.get();
        System.out.println("异步任务成功完成了...结果是：" + integer);
    }

    public static void fun3() throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).handle((result, thr) -> {
            if (result != null) {

                return result * 2;
            }
            if (thr != null) {

                return 0;
            }
            return 0;
        });
        Integer integer = future.get();
        System.out.println("异步任务成功完成了...结果是：" + integer);
    }

    public static void fun4() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenRunAsync(() -> {
            System.out.println("任务二当前线程：" + Thread.currentThread().getId());
            System.out.println("任务2启动了...");
        }, executor);
        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenAcceptAsync(res -> {
            System.out.println("任务二当前线程：" + Thread.currentThread().getId());
            System.out.println("任务2启动了..." + res);
        }, executor);

        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");

        CompletableFuture<Integer> voidCompletableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenApplyAsync(res -> {
            System.out.println("任务二当前线程：" + Thread.currentThread().getId());
            return res + 1;
        }, executor);
        System.out.println(voidCompletableFuture2.get());
    }

    public static void fun5() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> Future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务一运行结果：" + i);
            return i;
        }, executor);
        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");
        CompletableFuture<String> Future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二当前线程：" + Thread.currentThread().getId());
            String str = "hello";
            System.out.println("任务二当前线程运行结果：" + str);
            return str;
        }, executor);
        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");
        Future1.runAfterBothAsync(Future2, () -> {
            System.out.println("任务三runAfterBothAsync");
        }, executor);
        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");
        Future1.thenAcceptBothAsync(Future2, (F1, F2) -> {
            System.out.println("F1:  " + F1 + "F2:  " + F2);
            System.out.println("任务三thenAcceptBothAsync");
        }, executor);
        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");
        CompletableFuture<String> runAfterBothAsync = Future1.thenCombineAsync(Future2, (F1, F2) -> {
            return F1 + F2;
        }, executor);
        System.out.println("任务三runAfterBothAsync  :  " + runAfterBothAsync.get());
    }

    public static void fun6() throws ExecutionException, InterruptedException {
        CompletableFuture<String> Future1 = CompletableFuture.supplyAsync(() -> {
            sleep(0.5);
            System.out.println("查询商品信息1");
            return "一";
        }, executor);

        CompletableFuture<String> Future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品信息2");
            return "二";
        }, executor);

        CompletableFuture<String> Future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品信息3");
            return "三";
        }, executor);
        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(Future1, Future2, Future3);
        voidCompletableFuture.get();//阻塞voidCompletableFuture里面所有的任务
        System.out.println(Future1.get()+""+Future2.get()+""+Future3.get());

        sleep(1);
        System.out.println("``````````````````````````````````````````````````````````");

        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(Future1, Future2, Future3);
        System.out.println(objectCompletableFuture.get());//获取一个任务后，阻塞objectCompletableFuture里面其他所有的任务

    }
}
